package com.ym;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ym.dto.ApiBaseRequestDTO;
import com.ym.dto.PayStatusDTO;
import com.ym.dto.WxPayDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author ym
 * @date 2024/11/19
 * @description
 */
public class Demo {


    private static String url = "http://ywym-api.qidianpw.com/wx-pay"; // 正式环境
    // private static String url = "http://ywymtest-api.azhiyun.cn/wx-pay"; //测试环境
    private static String ymId = "";
    private static String ymKey = "";
    private static Long taxId = 1L;


    /**
     * 请求公钥
     */
    private static String reqPuk = "";


    private static String respPuk = "";

    private static String respPir = "";


    public static void main(String[] args) {

        transfer(); // 转账 (商家转账到零钱)
        // transferInfo(); // 查询交易结果
        // transferBatchTest(); //并发测试
    }

    /**
     * 并发测试
     */
    public static void transferBatchTest() {

        List<ApiBaseRequestDTO> list = new ArrayList<>();

        for (int i = 0; i < 15; i++) {

            ApiBaseRequestDTO apiBaseRequestDTO = new ApiBaseRequestDTO();
            WxPayDTO wxPayDTO = new WxPayDTO();
            wxPayDTO.setTaxId(taxId);
            // wxPayDTO.setBatchNo("test-" + System.currentTimeMillis());
            wxPayDTO.setBatchNo(getUuid());
            wxPayDTO.setAppid("wxb8516c6064271fe4");
            wxPayDTO.setBatchName("测试批次");
            wxPayDTO.setBatchRemark("测试打款");
            wxPayDTO.setTotalAmount(100);
            wxPayDTO.setTotalNum(1);
            // wxPayDTO.setTransferSceneId();
            // wxPayDTO.setNotifyUrl();

            List<WxPayDTO.WxTransferDetail> wxTransferDetailList = new ArrayList<>();

            WxPayDTO.WxTransferDetail wxTransferDetail = new WxPayDTO.WxTransferDetail();
            // wxTransferDetail.setTransferNo("test-info-" + System.currentTimeMillis());
            wxTransferDetail.setTransferNo(getUuid());
            wxTransferDetail.setTransferAmount(100);
            wxTransferDetail.setTransferRemark("红包");
            // wxTransferDetail.setOpenid("oCZuC5RiJ8RId4qacC0inhy74vRc");
            wxTransferDetail.setOpenid("oCZuC5RiJ8RId4qacC0inhy74vrc");
            // wxTransferDetail.setUserName("");
            wxTransferDetailList.add(wxTransferDetail);
            wxPayDTO.setTransferDetailList(wxTransferDetailList);
            apiBaseRequestDTO.setData(RsaUtils.encrypt(JSONObject.toJSONString(wxPayDTO), reqPuk));
            list.add(apiBaseRequestDTO);

            System.err.println("批次id:" + wxPayDTO.getBatchNo());
            System.err.println("明细id:" + wxTransferDetail.getTransferNo());
        }

        // for (ApiBaseRequestDTO apiBaseRequestDTO : list) {
        //     ThreadPoolUtils.execute(() -> {
        //         HttpRequest request = HttpUtil.createPost(url + "/transfer");
        //         // 请求头
        //         request.header("ym-id", ymId)
        //                 .header("ym-key", ymKey);
        //         String post = request.body(JSONObject.toJSONString(apiBaseRequestDTO)).contentType("application/json").execute().body();
        //         System.out.println(post);
        //
        //     });
        //
        // }

    }


    // 转账 (商家转账到零钱)
    public static void transfer() {
        ApiBaseRequestDTO apiBaseRequestDTO = new ApiBaseRequestDTO();
        WxPayDTO wxPayDTO = new WxPayDTO();
        wxPayDTO.setTaxId(taxId);
        // wxPayDTO.setBatchNo("test-" + System.currentTimeMillis());
        wxPayDTO.setBatchNo(getUuid());
        wxPayDTO.setAppid("wxb8516c6064271fe4");
        wxPayDTO.setBatchName("测试批次");
        wxPayDTO.setBatchRemark("测试打款");
        wxPayDTO.setTotalAmount(100);
        wxPayDTO.setTotalNum(1);
        // wxPayDTO.setTransferSceneId();
        // wxPayDTO.setNotifyUrl();

        List<WxPayDTO.WxTransferDetail> wxTransferDetailList = new ArrayList<>();

        WxPayDTO.WxTransferDetail wxTransferDetail = new WxPayDTO.WxTransferDetail();
        // wxTransferDetail.setTransferNo("test-info-" + System.currentTimeMillis());
        wxTransferDetail.setTransferNo(getUuid());
        wxTransferDetail.setTransferAmount(30);
        wxTransferDetail.setTransferRemark("红包");
        wxTransferDetail.setOpenid("oCZuC5RiJ8RId4qacC0inhy74vRc");// 真实的openid
        // wxTransferDetail.setOpenid("oCZuC53iJ84I245acC1inhy74CRc");//不存在的openid
        // wxTransferDetail.setUserName("");
        wxTransferDetailList.add(wxTransferDetail);
        wxPayDTO.setTransferDetailList(wxTransferDetailList);
        apiBaseRequestDTO.setData(RsaUtils.encrypt(JSONObject.toJSONString(wxPayDTO), reqPuk));

        System.out.println(JSONObject.toJSONString(apiBaseRequestDTO));
        HttpRequest request = HttpUtil.createPost(url + "/transfer");

        // 请求头
        request.header("ym-id", ymId)
                .header("ym-key", ymKey);
        String post = request.body(JSONObject.toJSONString(apiBaseRequestDTO)).contentType("application/json").execute().body();
        System.out.println(post);

        System.err.println("批次id:" + wxPayDTO.getBatchNo());
        System.err.println("明细id:" + wxTransferDetail.getTransferNo());

    }


    // 查询交易结果
    public static void transferInfo() {

        ApiBaseRequestDTO apiBaseRequestDTO = new ApiBaseRequestDTO();

        PayStatusDTO payStatusDTO = new PayStatusDTO();
        payStatusDTO.setBatchNo("test-1726737073069");
        payStatusDTO.setTransferNo("test-info-1726737073074");

        apiBaseRequestDTO.setData(RsaUtils.encrypt(JSONObject.toJSONString(payStatusDTO), reqPuk));

        System.out.println(JSONObject.toJSONString(apiBaseRequestDTO));
        HttpRequest request = HttpUtil.createPost(url + "/transfer-info");

        // 请求头
        request.header("ym-id", ymId)
                .header("ym-key", ymKey);
        String post = request.body(JSONObject.toJSONString(apiBaseRequestDTO)).contentType("application/json").execute().body();
        System.out.println(post);

        JSONObject jsonObject = JSONObject.parseObject(post);
        String data = RsaUtils.decrypt(jsonObject.get("data").toString(), respPir);
        System.out.println(data);

    }

    public static String getUuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }


}
